package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.PackageService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.packageInfo.pojo.dto.BaseInfoDTO;
import com.xbongbong.pro.packageInfo.pojo.dto.PackageLimitExceededDTO;
import com.xbongbong.pro.packageInfo.pojo.dto.PackageValidDTO;
import com.xbongbong.pro.packageInfo.pojo.dto.SystemCapacityListDTO;
import com.xbongbong.pro.packageInfo.pojo.dto.ValueAddedInfosDTO;
import com.xbongbong.pro.packageInfo.pojo.vo.BaseInfoVO;
import com.xbongbong.pro.packageInfo.pojo.vo.PackageLimitExceededVO;
import com.xbongbong.pro.packageInfo.pojo.vo.PackageLimitTipVO;
import com.xbongbong.pro.packageInfo.pojo.vo.PackageValidVO;
import com.xbongbong.pro.packageInfo.pojo.vo.SystemCapacityListVO;
import com.xbongbong.pro.packageInfo.pojo.vo.ValueAddedInfoVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 套餐
 * 创建时间 2019/4/4 2:19 PM
 * 修改时间 2019/4/4 2:19 PM
 * @author chy
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/package")
public class PackageController {

    @Resource
    private PackageService packageService;

    /**
     * 获取基础套餐信息
     * @param baseInfoDTO
     * @param br
     * @return
     * 创建时间 2019/4/4 4:19 PM
     * 修改时间 2019/4/4 4:19 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/baseInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String baseInfo(@RequestBody @Valid BaseInfoDTO baseInfoDTO, BindingResult br) {
        XbbResponse<BaseInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseInfoVO baseInfoVO = packageService.getBasePackageInfo(baseInfoDTO);
                response = new XbbResponse<>(baseInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取增值套餐信息
     * @param valueAddedInfoDTO
     * @param br
     * @return
     * 创建时间 2019/4/4 4:19 PM
     * 修改时间 2019/4/4 4:19 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/valueAddedInfos", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String valueAddedInfos(@RequestBody @Valid ValueAddedInfosDTO valueAddedInfoDTO, BindingResult br) {
        XbbResponse<ValueAddedInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ValueAddedInfoVO baseInfoVO = packageService.getValueAddedInfos(valueAddedInfoDTO);
                response = new XbbResponse<>(baseInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 右上角 我的中的部分套餐信息
     * @param systemCapacityListDTO
     * @param br
     * @return
     * 创建时间 2019/4/4 4:19 PM
     * 修改时间 2019/4/4 4:19 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/systemCapacityList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String systemCapacityList(@RequestBody @Valid SystemCapacityListDTO systemCapacityListDTO, BindingResult br) {
        XbbResponse<SystemCapacityListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemCapacityListVO systemCapacityListVO = packageService.getSystemCapacityList(systemCapacityListDTO);
                response = new XbbResponse<>(systemCapacityListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * http://yapi.xbongbong.com/project/87/interface/api/17162
     * 根据套餐校验自定义表单数量
     * @param packageValidDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/validate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String packageValidate(@RequestBody @Valid PackageValidDTO packageValidDTO, BindingResult br) throws Exception{
        XbbResponse<PackageValidVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PackageValidVO packageValidVO = packageService.packageValidate(packageValidDTO);
                response = new XbbResponse<>(packageValidVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * http://yapi.xbongbong.com/project/87/interface/api/17282
     * 获取套餐超出限制
     * @param packageLimitExceededDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/limitExceeded", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String limitExceeded(@RequestBody @Valid PackageLimitExceededDTO packageLimitExceededDTO, BindingResult br) throws Exception{
        XbbResponse<PackageLimitExceededVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PackageLimitExceededVO packageLimitExceededVO = packageService.limitExceeded(packageLimitExceededDTO);
                response = new XbbResponse<>(packageLimitExceededVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * http://yapi.xbongbong.com/project/87/interface/api/17290
     * 获取套餐超出限制
     * @param baseDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/limitTip", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String limitTip(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception{
        XbbResponse<PackageLimitTipVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PackageLimitTipVO packageLimitExceededVO = packageService.limitTip(baseDTO);
                response = new XbbResponse<>(packageLimitExceededVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
